package io.renren.framework.utils;

import io.renren.common.utils.JsonUtils;
import io.renren.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
public class KafkaProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void sendMessage(String topics, String key, Map message) {
        log.debug("消息队列,开始时间：" + System.currentTimeMillis());
        if (StringUtil.isEmpty(key)) {
            key = UUID.randomUUID().toString().replace("-", "");
        }
        kafkaTemplate.send(topics, key, JsonUtils.valueAsString(message));
        log.debug("消息队列,结束时间：" + System.currentTimeMillis());
    }


}
